import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [{
      path: 'recommended',
      name: 'recommended',
      component: () => import('../views/home/RecommendedView.vue')
    }, {
      path: 'feed',
      name: 'feed',
      component: () => import('../views/home/FeedView.vue')
    }, {
      path: 'notes',
      name: 'notes',
      component: () => import('../views/home/NotesView.vue')
    }, {
      path: 'videos',
      name: 'videos',
      component: () => import('../views/home/VideosView.vue')
    }, {
      path: 'events',
      name: 'events',
      component: () => import('../views/home/EventsView.vue')
    }, ]
  },
  {
    path: '/fooddetail/:foodid',
    name: 'fooddetail',
    component: () => import('../views/home/detail/FoodDetail.vue')
  },
  {
    path: '/notedetail/:noteid',
    name: 'notedetail',
    component: () => import('../views/home/detail/NoteDetail.vue')
  },
  {
    path: '/classroomdetail/:classid',
    name: 'classroomdetail',
    component: () => import('../views/classroom/ClassroomDetail.vue')
  },
  {
    path: '/classtag/:tagid',
    name: 'classtag',
    component: () => import('../views/classroom/ClassTagDetail.vue')
  },
  {
    path: '/classroom',
    name: 'classroom',
    component: () => import('../views/ClassroomView.vue'),
  },
  {
    path: '/store',
    name: 'store',
    component: () => import('../views/StoreView.vue'),
  },
  {
    path: "/myshop",
    name: "myshop",
    component: () => import('../views/store/MyShop.vue')
  },
  {
    path: "/storetag/:tagid",
    name: "storetag",
    component: () => import('../views/store/StoreTagDetail.vue')
  },
  {
    path: '/storedetail/:storeid',
    name: 'storedetail',
    component: () => import('../views/store/StoreDetail.vue')
  },
  {
    path: '/collection',
    name: 'collection',
    meta: {
      shouldLogin: true
    },
    component: () => import('../views/CollectionView.vue'),
    children: [{
      path: "cgroup",
      name: "cgroup",
      component: () => import('../views/collection/CollectionGroup.vue')
    }, {
      path: "crecipe",
      name: "crecipe",
      component: () => import('../views/collection/CollectionRecipe.vue')
    }, {
      path: "cnotes",
      name: "cnotes",
      component: () => import('../views/collection/CollectionNotes.vue')
    }, {
      path: "cclass",
      name: "cclass",
      component: () => import('../views/collection/CollectionClass.vue')
    }, ]
  },
  {
    path: '/mine',
    name: 'mine',
    meta: {
      shouldLogin: true
    },
    component: () => import('../views/MineView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: "",
    name: "start",
    redirect: "/home"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token");
  if (to.meta.shouldLogin && to.name !== 'login' && !token) {
    localStorage.setItem("lastPage", to.path);
    next({
      name: "login"
    })
  } else {
    next();
  }
})

export default router